package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "购物车")
@RestController
@RequestMapping("user/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查看购物车
     * @return
     */
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        log.info("开始查询购物车...");
        List<ShoppingCart> shoppingCartList= shoppingCartService.list();
        return Result.success(shoppingCartList);
    }

    /**
     * 添加购物车
     * @param dto
     * @return
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO dto){
        log.info("添加购物车信息：{}",dto);
        shoppingCartService.add(dto);
        return Result.success();
    }

    /**
     * 删除一个菜品或套餐
     * @param dto
     * @return
     */
    @ApiOperation("删除一个菜品或套餐")
    @PostMapping("/sub")
    public Result delete(@RequestBody ShoppingCartDTO dto){
        log.info("要删除的菜品或套餐信息：{}",dto);
        shoppingCartService.update(dto);
        return Result.success();
    }

    /**
     * 清空购物车
     * @return
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("clean")
    public Result deleteAll(){
        log.info("清空购物车....");
        shoppingCartService.deleteAll();
        return Result.success();
    }
}
